package gui.properties;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import model.MetroModel;
import model.objects.EdiTunnel;

public class TunnelPropertiesPanel extends FlowPanel {

	private EdiTunnel tunnel;
	public TunnelPropertiesPanel(EdiTunnel tunnel) {
		super("Line \"" + tunnel.getLine().getName() + "\"");
		this.tunnel = tunnel;
		this.setPanelComponents(this.initPanelComponents());
		
	}
	private List<Component> initPanelComponents() {
		List<Component> comps = new LinkedList<Component>();		
		
		JPanel toPanel = new JPanel();
		toPanel.setLayout(new GridLayout(0,2));
		toPanel.add(new JLabel("To:"));
		toPanel.add(new JLabel(tunnel.getToStation().getName()));
		
		
		
		
		JPanel fromPanel = new JPanel();
		fromPanel.setLayout(new GridLayout(0,2));
		fromPanel.add(new JLabel("From:"));
		fromPanel.add(new JLabel(tunnel.getFromStation().getName()));
		
		
		JPanel linePanel = new JPanel();
		linePanel.setLayout(new GridLayout(0,2));
		linePanel.add(new JLabel("Line:"));		
		linePanel.add(new JLabel("<HTML><FONT COLOR="+
				colorToHex(new Color(tunnel.getLine().getColor())) + ">" 
				+ tunnel.getLine().getName() + "</FONT></html>"));

		
		
		JPanel timePanel = new JPanel();
		timePanel.setLayout(new GridLayout(0,2));
		timePanel.add(new JLabel("Time:"));
		timePanel.add(new TimeField());
		
		JPanel smoothPanel = new JPanel();
		smoothPanel.setLayout(new GridLayout(0,2));
		smoothPanel.add(new JLabel("Smooth:"));
		smoothPanel.add(new SmoothRadio());
		
		comps.add(fromPanel);
		comps.add(toPanel);
		comps.add(linePanel);
		comps.add(timePanel);
		comps.add(smoothPanel);
		return comps;
	}
	
	private String colorToHex(Color color) {
		String c = "#";
		
		c+=numberToLetter(color.getRed()/16);
		c+=numberToLetter(color.getRed()%16);
		c+=numberToLetter(color.getGreen()/16);
		c+=numberToLetter(color.getGreen()%16);
		c+=numberToLetter(color.getBlue()/16);
		c+=numberToLetter(color.getBlue()%16);
		return c;
	}
	
	private char numberToLetter(int num) {
		
		switch(num) {
		case 0:
			return '0';
		case 1:
			return '1';
		case 2:
			return '2';
		case 3:
			return '3';
		case 4:
			return '4';
		case 5:
			return '5';
		case 6:
			return '6';
		case 7:
			return '7';
		case 8:
			return '8';
		case 9:
			return '9';
		case 10:
			return 'A';
		case 11:
			return 'B';
		case 12:
			return 'C';
		case 13:
			return 'D';
		case 14:
			return 'E';
		case 15:
			return 'F';
		}
		return '-';
	}
	class TimeField extends JTextField {
		public TimeField() {
			//final JTextField nameField = this;
			setText(""+tunnel.getTime());
			addKeyListener(new KeyListener() {

				@Override
				public void keyPressed(KeyEvent arg0) {
					if(arg0.getKeyCode()==KeyEvent.VK_ENTER) {
						model.MetroModel.getInstance().setTunnelTime(tunnel, Integer.parseInt(getText()));
					}				
				}

				@Override
				public void keyReleased(KeyEvent arg0) {
					
					
				}

				@Override
				public void keyTyped(KeyEvent arg0) {
					
				}
				
			});
			
		}
		
	}
	
	class SmoothRadio extends JCheckBox {
		public SmoothRadio() {
			//final JTextField nameField = this;
			this.setSelected(tunnel.isSmooth());
			
			this.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					tunnel.setSmooth(!tunnel.isSmooth());
					MetroModel.getInstance().modelChanged(false);
				}
				
			});
		}
					
	}
	

}
